/* ChatMessageFormatter.java
 *
 * Copyright 2011-2012 Aleksey Konovalov, r.u.s.h.a.n.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 * 
 */

package org.jimm.app;

import java.util.HashMap;
import java.util.Map.Entry;

import org.jimm.message.PlainMessage;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;

/**
 * РєР»Р°СЃСЃ РґР»СЏ С„РѕСЂРјР°С‚РёСЂРѕРІР°РЅРёСЏ СЃС‚СЂРѕРє РІ РѕРєРЅРµ РїРµСЂРµРїРёСЃРєРё
 *
 * @author РђР»РµРєСЃРµР№ РљРѕРЅРѕРІР°Р»РѕРІ (aleks_dsp)
 *         <p/>
 *         Р�СЃРїРѕР»СЊР·РѕРІР°РЅ РєРѕРґ
 *         http://stackoverflow.com/questions/3341702/displaying-
 *         emoticons-in-android/4302199#4302199
 */
public class ChatMessageFormatter {

    private static final ForegroundColorSpan
            INCOMING_MESSAGE_FOREGROUND_COLOR_SPAN = new ForegroundColorSpan(Color.LTGRAY);
    private static final ForegroundColorSpan
            OUTGOING_MESSAGE_FOREGROUND_COLOR_SPAN = new ForegroundColorSpan(Color.LTGRAY);

    private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();

    static {
        emoticons.put(":)", R.drawable.smile_smile);
        emoticons.put(":-)", R.drawable.smile_smile);
        emoticons.put("=-)", R.drawable.smile_smile);
        emoticons.put("=)", R.drawable.smile_smile);
        emoticons.put(":]", R.drawable.smile_happy);
        emoticons.put(":-]", R.drawable.smile_happy);
        emoticons.put("*happy*", R.drawable.smile_happy);
        emoticons.put(":-(", R.drawable.smile_sad);
        emoticons.put(":(", R.drawable.smile_sad);
        emoticons.put("=-(", R.drawable.smile_sad);
        emoticons.put("=(", R.drawable.smile_sad);
        emoticons.put(":-P", R.drawable.smile_tongue);
        emoticons.put(":P", R.drawable.smile_tongue);
        emoticons.put("=P", R.drawable.smile_tongue);
        emoticons.put("=-P", R.drawable.smile_tongue);
        emoticons.put(":'(", R.drawable.smile_cry);
        emoticons.put(":-[", R.drawable.smile_blush);
        emoticons.put(":[", R.drawable.smile_blush);
        emoticons.put("*kissed*", R.drawable.smile_kissed);
        emoticons.put("*hi*", R.drawable.smile_hi);
        emoticons.put("*hello*", R.drawable.smile_hi);
        emoticons.put("*sorry*", R.drawable.smile_sorry);
        emoticons.put("*thumbs up*", R.drawable.smile_up);
        emoticons.put("*thumbs_up*", R.drawable.smile_up);
        emoticons.put(";-)", R.drawable.smile_wink);
        emoticons.put(";)", R.drawable.smile_wink);

        emoticons.put("*troll*", R.drawable.smile__troll);
        emoticons.put("*okey*", R.drawable.smile__okey);
        emoticons.put("*xD*", R.drawable.smile__xd);
        emoticons.put("*lol*", R.drawable.smile__lol);
        emoticons.put("*hmm*", R.drawable.smile__hmm);
    }

    /**
     * Р�С‰РµС‚ СЃРјР°Р№Р»С‹ РІ СЃС‚СЂРѕРєРµ Рё РґРѕР±Р°РІР»СЏРµС‚ РёС…
     *
     * @param context
     * @param builder  СЃС‚СЂРѕРєР°
     * @param startPos РїРѕР·РёС†РёСЏ СЃРёРјРІРѕР»Р° РІ С‚РµРєСЃС‚Рµ, РЅР°С‡РёРЅР°СЏ СЃ РєРѕС‚РѕСЂРѕРіРѕ РёРґРµС‚ РїРѕРёСЃРє
     *                 СЃРјР°Р№Р»РѕРІ
     * @param endPos   РїРѕР·РёС†РёСЏ СЃРёРјРІРѕР»Р° РІ С‚РµРєСЃС‚Рµ, РґРѕ РєРѕС‚РѕСЂРѕРіРѕ РёРґРµС‚ РїРѕРёСЃРє СЃРјР°Р№Р»РѕРІ
     */
    private static void detectEmotions(Context context,
                                       SpannableStringBuilder builder, int startPos, int endPos) {
        for (int index = startPos; index < endPos; ++index) {
            for (Entry<String, Integer> entry : emoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString()
                        .equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()),
                            index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
    }

    public static Spannable getFormattedText(Context context, PlainMessage message) {

        String text = message.getText();

        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        detectEmotions(context, builder, 0, text.length());
        builder.setSpan(
                message.isIncoming() ? INCOMING_MESSAGE_FOREGROUND_COLOR_SPAN
                        : OUTGOING_MESSAGE_FOREGROUND_COLOR_SPAN, 0, text.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return builder;
    }
}
